package com.zz.util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zz.dto.OrderDTO;
import com.zz.entity.ShopRecord;
import com.zz.exception.DataValidationException;
import com.zz.form.OrderForm;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.DataFormatException;

/**
 * Created by Bsea
 * 2017-06-18 23:41
 */
@Slf4j
public class OrderForm2OrderDTOConverter {

    public static OrderDTO convert(OrderForm orderForm) {
        Gson gson = new Gson();
        OrderDTO orderDTO = new OrderDTO();
        orderDTO.setShopOrderCode(orderForm.getShopOrderCode());
        orderDTO.setShopOrderName(orderForm.getShopOrderName());
        orderDTO.setShopOrderRemarks(orderForm.getShopOrderRemarks());
        orderDTO.setShopOrderCompanyid(orderForm.getShopOrderCompanyid());
        orderDTO.setShopOrderUser(orderForm.getShopOrderUser());
        orderDTO.setShopOrderStatus(orderForm.getShopOrderStatus());

        List<ShopRecord> orderDetailList = new ArrayList<>();
        try {
            orderDetailList = gson.fromJson(orderForm.getShopRecordList(),
                    new TypeToken<List<ShopRecord>>() {
                    }.getType());
        } catch (Exception e) {
            log.error("【对象转换】错误, string={}", orderForm.getShopRecordList());
            throw new DataValidationException("对象转换");
        }
        orderDTO.setOrderDetailList(orderDetailList);

        return orderDTO;
    }
}
